/**
 * 获取栏目下的栏目
 * @param obj {TYPEID, ?LENGTH, ?STATION}  
 */
 function getTypeListByTypeId(obj) {
  if (!obj) {
    console.log('getTypeListByTypeId 缺少参数');
  }
  var TYPEID = obj.TYPEID;
  var LENGTH = obj.LENGTH ? obj.LENGTH : 99;
  var STATION = obj.STATION ? obj.STATION : 0;
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getTypeListByTypeId?TYPEID=' + TYPEID + '&LENGTH=' + LENGTH + '&STATION=' + STATION,
    success: function (data) {
      _data = data;
    },
    error: function () {
      console.log('getTypeListByTypeId 接口错误');
    }
  })
  return _data;
}

/**
 * 获取栏目下的节目
 * @param obj {TYPEID, ?LENGTH, ?STATION}
 */
function getVodListByTypeId(obj) {
  if (!obj) {
    console.log('getVodListByTypeId 缺少参数');
  }
  var TYPEID = obj.TYPEID;
  var LENGTH = obj.LENGTH ? obj.LENGTH : 99;
  var STATION = obj.STATION ? obj.STATION : 0;
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getVodListByTypeId?TYPEID=' + TYPEID + '&LENGTH=' + LENGTH + '&STATION=' + STATION,
    success: function (data) {
      _data = data;
    },
    error: function () {
      console.log('getVodListByTypeId 接口错误');
    }
  })
  return _data;
}


/**
 * 获取专题栏目下的节目
 * @param obj {TYPEID, ?LENGTH, ?STATION}
 */
function getSpecialVodListByTypeId(obj) {
    if (!obj) {
        console.log('getSpecialVodListByTypeId 缺少参数');
    }
    var TYPEID = obj.TYPEID;
    var LENGTH = obj.LENGTH ? obj.LENGTH : 99;
    var STATION = obj.STATION ? obj.STATION : 0;
    var _data = null;
    $.ajax({
        type: 'get',
        async: false,
        url: config.api + '/restApi/getSpecialVodListByTypeId?TYPEID=' + TYPEID + '&LENGTH=' + LENGTH + '&STATION=' + STATION,
        success: function(data) {
          _data = data;
        },
        error: function() {
          console.log('getSpecialVodListByTypeId 接口错误');
        }
    })
    return _data;
}



/**
 * 获取节目详情
 * @param {VODID} str 
 */
function getVodDetailInfo(id) {
  if (!id) {
    console.log('getVodDetailInfo 缺少参数');
  }

  var _data = null;
  var account = config.userid;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getVodDetailInfo?account=' + account + '&VODID=' + id,
    success: function (data) {
      _data = data;
    },
    error: function () {
      console.log('getVodDetailInfo 接口错误');
    }
  })
  return _data;
}

/**
 * 获取栏目详情
 * @param {id} str 
 */
function getTypeDetailInfo(id) {
  var _data = null;
  if (!id) {
    console.log('getTypeDetailInfo 缺少参数');
  }
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getTypeDetailInfo?TYPEID=' + id,
    success: function (data) {
      _data = data;
    },
    error: function () {
      console.log('getTypeDetailInfo 接口错误');
    }
  })
  return _data;
}

/**
 * 探针获取
 * @param {programId, programName, startTime, endTime} obj 
 */
function getLog(params) {
  if (!params.length) {
    return
  }
  // var 
  var programId = params[14]
  var programName = params[8] + '-' + params[5] + '-' + params[6]
  var ctype =  '1'  // 栏目 1  节目  2
  var platform = config.platform
  var account = config.userid
  var usermac = params[0]
  var otherParams= ''
  if (params[10]) {
    ctype = '2'
    otherParams = '&startTime=' + params[9] + '&endTime=' + params[10]
  }
  $.ajax({
    type: 'get',
    // async: false,
    url: config.api + '/restApi/visit?programId=' + programId + '&programName=' +
      programName + '&ctype=' + ctype + '&account=' + account + '&usermac=' +
      usermac + '&platform=' + platform + otherParams,
    success: function (data) {
      // _data = data;
    },
    error: function () {
      console.log('getLog 接口错误');
    }
  })
  // return _data;
}


/**
 * EPG下单成功保存记录。此处记录不准确（用户购买成功后无回调），当用户从订购页面点击时记录
 * @param {programId, programName, ctype} obj 
 */
function addOrder(obj) {
  if (!obj) {
    console.log('addOrder 缺少参数');
  }
  var programId = obj.programId ? obj.programId : '';
  var programName = obj.programName ? obj.programName : '';
  var ctype = obj.ctype ? obj.ctype : ''; // 套餐名称
  var usermac = obj.usermac ? obj.usermac : '';;
  var platform = config.platform;
  var account = config.userid;
  if(!programId || !programName){return}
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/addOrder?programId=' + programId + '&programName=' +
      programName + '&ctype=' + ctype + '&account=' + account + '&usermac=' +
      usermac + '&platform=' + platform,
    success: function (data) {
      // _data = data;
    },
    error: function () {
      console.log('addOrder 接口错误');
    }
  })
}


/**
 * 添加地址: 记住上一个页面地址记到数据库，EPG返回使用
 * @param page str 
 */
function addReturnPage(page) {
  // if (!page) {
  //   console.log('addReturnPage 缺少参数');
  // }
  page = escape(page)
  var platform = config.platform;
  var account = config.userid;
  var time = new Date().getTime()
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/addReturnPage?platform=' + platform + '&account=' + account + '&page=' + page + '&gmtCreate=' + time,
    success: function (data) {
      // _data = data;
    },
    error: function () {
      console.log('addReturnPage 接口错误');
    }
  })
}

/**
 * 获取地址: 用户点击返回键时跳转得页面
 */
function getReturnPage() {
  var platform = config.platform;
  var account = config.userid;
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getReturnPage?platform=' + platform + '&account=' + account,
    success: function (data) {
      _data = data;
    },
    error: function () {
      console.log('getReturnPage 接口错误');
    }
  })
  return _data
}


/**
 * 添加主页地址: 记录主页地址，调用广电接口（获取视频路径、鉴权、购买）、返回广电页面时用
 * @param {*} page 
 */
function addDomainPath(page) {
  if (!page) {
    console.log('addDomainPath 缺少参数');
  }
  var platform = config.platform;
  var account = config.userid;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/addDomainPath?platform=' + platform + '&account=' + account + '&page=' + page,
    success: function (data) {
      // _data = data;
    },
    error: function () {
      console.log('addDomainPath 接口错误');
    }
  })
}

/**
 * 获取主页地址： 获取主页地址，用广电接口（获取视频路径、鉴权、购买）、返回广电页面
 */
function getDomainPath() {
  var _data = null;

  var plaform = config.platform;
  var account = config.userid;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getDomainPath?platform=' + plaform + '&account=' + account,
    success: function (data) {
      _data = data;
    },
    error: function () {
      console.log('getDomainPath 接口错误');
    }
  })
  return _data;
}

/**
 * EPG搜索页面
 * @param {QUERY，LENGTH，STATION } obj 
 */
function search(obj) {
  // var platform = 1; // 这里platform代表专区{1：熊猫，2：文创}
  var _data = null;
  var QUERY = obj.QUERY;
  var LENGTH = obj.LENGTH ? obj.LENGTH : 8;
  var STATION = obj.STATION ? obj.STATION : 1;
  if (!QUERY) {
    console.log('search 缺少参数');
  }
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getVodListBySearchName?platform=1&searchName=' + QUERY + '&LENGTH=' + LENGTH + '&STATION=' + STATION,
    success: function (data) {
      _data = data;
    },
    error: function () {
      console.log('search 接口错误');
    }
  })
  return _data;
}

/**
 * 添加播放记录
 * @param {节目id} programId 
 * @param {节目名称} programName 
 */
function addPlayHistory(programId, programName) {
  // var platform = 1; // 这里platform代表专区{1：熊猫，2：文创}
  var account = config.userid;
  var usermac = config.mac;
  var platform = config.platform;
  if (!(programId && programName)) {
    return console.log('addPlayHistory 缺少参数');
  }
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/addPlayHistory?ctype=剧头&position=0&platform=' + platform + '&account=' + account + '&usermac=' + usermac + '&programId=' + programId + '&programName=' + programName,
    success: function (data) {
      // console.log(data)
    },
    error: function () {
      console.log('addPlayHistory 接口错误');
    }
  })
  // 只保存12条历史记录
  var total = getHistoryList(1).total;
  var data = getHistoryList(total);
  while (total > 12) {
    delPlayHistory(data.data[--total].programId);
  }
}


/**
 * 获取播放列表
 * @param {*} len 
 */
function getHistoryList(len) {
  var account = config.userid;
  // var platform = 1; // 这里platform代表专区{1：熊猫，2：文创}
  var len = len ? len : 12;
  var platform = config.platform;
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getHistoryList?platform=' + platform + '&LENGTH=' + len + '&STATION=1&account=' + account,
    success: function (data) {
      _data = data;
    },
    error: function () {
      console.log('search 接口错误');
    }
  })
  return _data;
}

/**
 * 删除播放纪录
 * @param {*} programId 
 */
function delPlayHistory(programId) {
  var account = config.userid;
  if (!programId) {
    return console.log('delPlayHistory 缺少参数');
  }
  $.ajax({
    type: 'get',
    url: config.api + '/restApi/delPlayHistory?account=' + account + '&programId=' + programId,
    success: function (data) {

    },
    error: function () {
      console.log('delPlayHistory 接口错误');
    }
  })
}

/**
 * 添加收藏
 * @param {*} programId 
 * @param {*} programName 
 */
function addFavorite(programId, programName) {
  var account = config.userid;
  var usermac = config.mac;
  // var platform = 1; // 这里platform代表专区{1：熊猫，2：文创}
  var plaform = config.platform;
  if (!(programId && programName)) {
    console.log('addFavorite 缺少参数');
  }
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/addFavorite?platform=' + platform + '&ctype=1&account=' + account + '&usermac=' + usermac + '&programId=' + programId + '&programName=' + programName,
    success: function (data) {
      _data = data;
    },
    error: function () {
      console.log('addFavorite 接口错误');
    }
  })
  return _data
}

/**
 * 取消收藏
 * @param {*} programId 
 */
function delFavorite(programId) {
  if (!programId) {
    console.log('delFavorite 缺少参数');
  }
  var account = config.userid;
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/delFavorite?programId=' + programId + '&account=' + account,
    success: function (data) {
      _data = data;

    },
    error: function () {
      console.log('delFavorite 接口错误');
    }
  })
  return _data
}


/**
 * 收藏列表
 * @param {length, page} obj 
 */
function getFavList(obj) {
  if (!obj) {
    console.log('getFavList 缺少参数');
  }
  var account = config.userid;
  // var platform = 1; // 这里platform代表专区{1：熊猫，2：文创}
  
  var plaform = config.platform;
  var LENGTH = obj.LENGTH ? obj.LENGTH : 12;
  var STATION = obj.PAGE ? obj.STATION : 1;
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getFavList?account=' + account + '&platform=' + platform + '&LENGTH=' + LENGTH + '&STATION=' + STATION,
    success: function (data) {
      _data = data;

    },
    error: function () {
      console.log('getFavList 接口错误');
    }
  })
  return _data
}

/**
 * 获取白名单
 * @param account
 */
function getSpecialAccountByaccount() {
  var account = config.userid;
  if (!account) {
    console.log('getFavList 缺少参数');
  }
  var _data = null;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getSpecialAccountByaccount?account=' + account,
    success: function (data) {
      _data = data;
    },
    error: function () {
      console.log('getSpecialAccountByaccount 接口错误');
    }
  })
  return _data
}


// 清空地址
function clearReturnPage() {
  var account = config.userid;
  var platform = config.platform;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/clearReturnPage?platform=' + platform + '&account=' + account,
    success: function (data) {},
    error: function () {
      return new Error('clearReturnPage接口错误!');
    }
  })
}

// 清空主页信息
function clearDomainPath() {
  
  var plaform = config.platform;
  $.ajax({
    type: 'get',
    url: config.api + '/restApi/clearDomainPath?platform=' + plaform + '&account=' + USERID,
    success: function (data) {
      _data = data;
    },
    error: function () {
      return new Error('getDomainPath接口错误!');
    }
  })
}

// 获取栏目轮播
function getLiveListByTypeId (TYPEID) {
  var data = null
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getLiveListByTypeId?TYPEID=' + TYPEID,
    success: function(res){
      data = res
    },
    errot: function (){
      return new Error('getLiveListByTypeId接口错误!')
    }
  })
  return data
}

// 投票
function voteColumn (TYPEID) {
  var data = null
  var account = config.userid;
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/voteColumn?account=' + account + '&columnId=' + TYPEID,
    success: function(res){
      data = res
    },
    errot: function (){
      return new Error('voteColumn接口错误!')
    }
  })
  return data
}

// 通过typeid获取tree
function getTreeByTypeId (TYPEID) {
  var data = null
  $.ajax({
    type: 'get',
    async: false,
    url: config.api + '/restApi/getTreeByTypeId?TYPEID=' + TYPEID,
    success: function(res){
      data = res
    },
    errot: function (){
      return new Error('getTreeByTypeId接口错误!')
    }
  })
  return data
}


/**
 * 签到加积分
 */
 function signPoints(params) {
  var account = config.userid;
  var points = params.points;
  var desc = params.desc;
  var _data = null;
  $.ajax({
      type: 'get',
      url: config.api + '/restApi/signPoints?account=' + account + '&points=' + points + '&desc=' + desc,
      async: false,
      success: function (data) {
          _data = data;
      },
      error: function () {
          return new Error('getDomainPath接口错误!');
      }
  })
  return _data;
}


/**
* 积分增加(完成任务)
* @param {points， desc} params 
* @returns 
*/
function taskPoints(params) {
  var account = config.userid;
  var points = params.points;
  var desc = params.desc;
  var _data = null;
  $.ajax({
      type: 'get',
      url: config.api + '/restApi/taskPoints?account=' + account + '&points=' + points + '&desc=' + desc,
      async: false,
      success: function (data) {
          _data = data;
      },
      error: function () {
          return new Error('getDomainPath接口错误!');
      }
  })
  return _data;
}

/**
* 积分增加(购买月卡)
* @param {points， desc} params 
* @returns 
*/
function consumePoints(params) {
  var account = config.userid;
  var points = params.points;
  var desc = params.desc;
  var _data = null;
  $.ajax({
      type: 'get',
      url: config.api + '/restApi/consumePoints?account=' + account + '&points=' + points + '&desc=' + desc,
      async: false,
      success: function (data) {
          _data = data;
      },
      error: function () {
          return new Error('getDomainPath接口错误!');
      }
  })
  return _data;
}

/**
* 积分订单
* @param {programId,programName,account, phone} params 
* @returns 
*/
function orderPoints(data) {
  var account = config.userid;
  data.account = account
  var _data = null;
  $.ajax({
      type: 'post',
      url: config.api + '/restApi/orderPoints',
      async: false,
      data: data,
      success: function (data) {
          _data = data;
      },
      error: function () {
          return new Error('getDomainPath接口错误!');
      }
  })
  return _data;
}

/**
* 签到次数
* @param 
* @returns 
*/
function countSign() {
  var account = config.userid;
  var _data = null;
  $.ajax({
      type: 'get',
      url: config.api + '/restApi/countSign?account=' + account,
      async: false,
      success: function (data) {
          _data = data;
      },
      error: function () {
          return new Error('getDomainPath接口错误!');
      }
  })
  return _data;
}


/**
* 获取用户积分
*/
function getPoints() {
  var account = config.userid;
  var _data = null;
  $.ajax({
      type: 'get',
      url: config.api + '/restApi/getPoints?account=' + account,
      async: false,
      success: function (data) {
          _data = data;
      },
      error: function () {
          return new Error('getDomainPath接口错误!');
      }
  })
  return _data;
}


function encryption (param, aesKey) {
  var data = null
  aesKey = aesKey ? aesKey : '7A1396D5BC9A910E17F49DF2F756B570' 
  $.ajax({
    type: 'get',
    url: config.api + '/restApi/encryption?aesKey=' + aesKey + '&param=' + param,
    async: false,
    success: function (res) {
        data = res.data;
    },
    error: function () {
        return new Error('encryption error');
    }
  })
  return data
}
//移动使用
function visitlogApi(obj) {
  if (!obj) {
      Test.showLine('visitlogApi 缺少参数');
  }
  var type = obj.type;
  var account = obj.account;
  var _data = null;
  $.ajax({
      type: 'get',
      async: false,
      url: config.api + '/restApi/visitLog?type=' + type + '&account=' + account,
      success: function(data) {
          _data = data;
      },
      error: function() {
          Test.showLine('visitlogApi 接口错误');
      }
  })
  return _data;


}